package cn.ffcs.ref.web;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.ref.model.ProductRefSort;
import cn.ffcs.ref.service.ProductRefSortService;
import cn.ffcs.util.MyList;

public class ProductRefSortAction<D extends ProductRefSort> extends BaseAction<D>
{
    /** 动态网路标识 */
    private static final long serialVersionUID = -8246991977511117520L;

    /** 福建省产品目录 **/
    private D productRefSort;
    
    /** 福建省产品目录结果集 **/
    private List<D> productRefSorts;
    /** 产品目录关联产品服务 **/
    private ProductRefSortService productRefSortService;
    /** 产品上级目录ID，用来记忆上级目录方便**/
    private Integer parentSort;

    public Integer getParentSort()
    {
        return parentSort;
    }

    public void setParentSort(Integer parentSort)
    {
        this.parentSort = parentSort;
    }

    /**
     * 新增或修改福建省产品目录类型数据
     * @return
     */
    
    public String saveOrUpdate()
    {
        ActionContext ctx = ActionContext.getContext();        
        HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 
        String[] ids=null;
        if(request.getParameter("refIds")!=null&&!request.getParameter("refIds").equals("")){
            ids=request.getParameter("refIds").split(",");
        }
       // baseService.deleteBy("ProductRefSort", "sort", productRefSort.getSort());
        productRefSortService.deleteBySort(productRefSort.getSort());
        if (ids!=null && ids.length>0){
            for(String productId:ids){
                ProductRefSort p=new ProductRefSort();
                p.setId(null);
                p.setSort(productRefSort.getSort());
                p.setCreator("admin");
                p.setBegin(new Date());
                p.setProduct(productId);
                productRefSortService.saveOrUpdate(p);
            }
        }
        //lProductRefSortType=dictTypeService.findAll();  
        returnMsg="保存操作已成功！";
        return SUCCESS;
    }

    /**
     * 删除数据
     * @return
     */
    public String remove()
    {
        baseService.remove(productRefSort);
        returnMsg="删除操作已成功！";
        return SUCCESS;
    }

    /**
     * 批量删除
     * @return
     */
    public String batchDel()
    {
        String[] ids = getContextObjects("ids");
        baseService.batchDel(ids);
        returnMsg="批量删除操作已成功！";
        return SUCCESS;
    }

    /**
     * 根据关键值获取实体数据
     * @return
     */
    public String findById()
    {
        if (productRefSort != null && productRefSort.getId() != null&& productRefSort.getId().intValue() !=0)
        {
            productRefSort = (D) baseService.findById(productRefSort.getId());
        }
        //lProductRefSortType=dictTypeService.findAll();        
        
        return SUCCESS;
    }

    /**
     * 获取福建省产品目录类型
     * @return
     */
    public D getProductRefSort()
    {
        return productRefSort;
    }
    /**
     * 产品类型关联视图
     * @return
     */
    public String findNextView(){
        ActionContext ctx = ActionContext.getContext();        
        HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   
        productRefSorts=super.findBy();
        String xml=MyList.getAll(productRefSorts);
        response.setCharacterEncoding("gb2312");//页面编码设置
        response.setContentType("text/xml");//页面文件头格式
//        System.out.println(xml);
        try{
            response.getWriter().write(xml);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 产品类型关联视图:根据产品名称查询-pany
     * @return
     */
    public String findNextView2(){
        ActionContext ctx = ActionContext.getContext();        
        HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   
        try{
            HashMap<String, Object> oldConds=super.getConds();
            if(oldConds!=null){
                HashMap<String, Object> newConds=new HashMap();
                Iterator i = oldConds.entrySet().iterator();
                while (i.hasNext())
                {
                    Entry entry = (Entry)i.next();
                    String key=(String)entry.getKey();
                    if(oldConds.get(key)!=null){
                        String str=oldConds.get(key).toString();
                        str=new String(str.getBytes("iso8859-1"),"utf-8");
                        if(str!=null&&!str.equals("")){
                            newConds.put(key,  str);
                        }
                    }
                }
                super.setConds(newConds);
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        productRefSorts = baseService.findBy(combineCond());
        String xml=MyList.getAll(productRefSorts);
        response.setCharacterEncoding("gb2312");//页面编码设置
        response.setContentType("text/xml");//页面文件头格式
//        System.out.println(xml);
        try{
            response.getWriter().write(xml);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    

    /**
     * 
     * 组合查询条件
     * @return
     */
    public Criterion combineCond(){
        Criterion cond = null;
        if (super.getConds() != null)
        {
            //遍历参数表
            Iterator i = super.getConds().entrySet().iterator();
            while (i.hasNext())
            {
                Entry entry = (Entry)i.next();
                String key=(String)entry.getKey();
                if (super.getConds().get(key) != null
                        && !StringUtils.isEmpty(super.getConds().get(key).toString()))
                {
                    //根据实际情况做处理
                    Criterion temp=null;
                    if (key.equalsIgnoreCase("productName")){
                        temp=Restrictions.like(key, "%"+super.getConds().get(key).toString()+"%");
                    }else{
                        temp=Restrictions.eq(key, super.getConds().get(key));                        
                    }
                    
                    if (cond == null)
                    {
                        cond = temp;
                    }
                    else
                    {
                        cond = Restrictions.and(cond, temp);
                    }
                }
            }
        }
        return cond;
    }
    
    /**
     * 设置福建省产品目录类型
     * @param dict
     */
    public void setProductRefSort(D product)
    {
        this.productRefSort = product;
    }

    /**
     * 获取结果集
     * @return
     */
    public List<D> getProductRefSorts()
    {
        return productRefSorts;
    }

    /**
     * 设置结果集
     * @param dicts
     */
    public void setProductRefSorts(List<D> productRefSorts)
    {
        this.productRefSorts = productRefSorts;
    }

    /**
     * @return the productRefSortService
     */
    public ProductRefSortService getProductRefSortService()
    {
        return productRefSortService;
    }

    /**
     * @param productRefSortService the productRefSortService to set
     */
    public void setProductRefSortService(ProductRefSortService productRefSortService)
    {
        this.productRefSortService = productRefSortService;
    }




}
